#ifndef UTMANAGER_H
#define UTMANAGER_H


#include <vector>
#include "../core/String.h"


template <class T>
struct UTManagerItem
{
	UTManagerItem()  { Data = 0; }
	~UTManagerItem() { delete Data; }
	String 	Name;
	T*		Data;
	int     Counter;
};


template <class T>
class UTManager
{
public:
	UTManager()
	{
	}

	~UTManager()
	{
		destroyAll();
	}

	T* create(const String& name, T* data)
	{
		if (data == NULL)
			return NULL;
		TItem* item = new TItem();
		item->Name = name;
		item->Data = data;
		item->Counter = 1;
		m_List.push_back(item);
		return item->Data;
	}

	void destroyAll()
	{
		int n = m_List.size();
		for (int i = 0; i < n; i++)
		{
			delete m_List[i];
		}
		m_List.clear();
	}

	int find(const String& name)
	{
		int n = m_List.size();
		for (int i = 0; i < n; i++)
		{
			if (m_List[i]->Name == name)
				return i;
		}
		return -1;
	}

	T* get(int index)
	{
		return m_List[index]->Data;
	}

    void increase(int index)
    {
        m_List[index]->Counter++;
    }

    int decrease(int index)
    {
        return --m_List[index]->Counter;
    }

private:
	typedef UTManagerItem<T> TItem;
	std::vector<TItem*> m_List;

};


#endif // UTMANAGER_H

